package com.easyui.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.easyui.bean.*;
import com.easyui.common.*;

public class GetStypeServlet extends HttpServlet {

	public GetStypeServlet() {}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/json;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		List<Map> stypeList = null;
		JSONArray jsonStype = null;
		
		HttpSession session = request.getSession(); //获的session
		String role = (String)session.getAttribute("role");
		String tno = (String)session.getAttribute("userID");
		
		SelStudent ss = new SelStudent();
		if(role != null && role.trim().equals("admin")) {
			if(tno != null && !tno.trim().equals("")) {
				jsonStype = new JSONArray();//装分工信息
				
				//插入基本类型
				JSONObject designer = new JSONObject();
				designer.put("id", "美工");
				designer.put("text", "美工");
				jsonStype.add(designer);
				
				JSONObject frontend = new JSONObject();
				frontend.put("id", "前端");
				frontend.put("text", "前端");
				jsonStype.add(frontend);
				
				JSONObject backend = new JSONObject();
				backend.put("id", "后台");
				backend.put("text", "后台");
				jsonStype.add(backend);
				
				stypeList = ss.getStype();
				if (stypeList != null && stypeList.size() > 0) {
					for (int i = 0; i < stypeList.size(); i++) {
						Map<String, String> stype = (Map)stypeList.get(i);
						String s = stype.get("stype").trim();
						if(!s.equals("美工") && !s.equals("前端") && !s.equals("后台")) {//不与默认类型重复
							JSONObject j = new JSONObject();
							j.put("id", s);
							j.put("text", s);
							jsonStype.add(j);
						}
					}
				}
				
			}
		}
		
		out.print(jsonStype);
		out.flush();
		out.close();
	}


}
